<template>
	<view>
		<view class="z-flex">
		  <image :src="cdn(config.user_image)" class="logo_img" mode=""/>
		</view>
		<view class="z-flex">
		<view class="z-font-w title">
		  {{config.user_name}}
		</view>
		
		</view>
		<view class=" z-flex z-margin-t-32 text_999 z-font-28">{{config.user_intro}}</view>
		
		<view class="z-flex btn-box" style="margin-top: 200rpx;" >
		  <!-- <button :open-type="getPhoneNumber" class="btn" @getphonenumber="getPhoneNumber">手机号快捷登录</button> -->
		  <button class="btn" @tap="getPhoneNumber">手机号快捷登录</button>
		  <view class="mask z-margin-l-32" @tap="read" v-if="!agree"></view>
		</view>
		<view class="z-flex" @tap="clickread">
		  <image src="../../static/login/choose.png"   v-if="!agree" class="choose_img" mode=""/>
		  <image src="../../static/login/choosed.png" v-else  class="choose_img" mode=""/>
		  <view class="text_999 z-font-28 z-margin-l-8">已阅读并同意 <text class="money" @tap.stop="infoTap('《用户协议》',1)">《用户协议》</text> 和 <text class="money" @tap.stop="infoTap('《隐私政策》',2)">《隐私政策》</text> </view>
		</view>
		<u-popup :show="show" mode="bottom" :closeable="true"  @close="onClose">
		  <view class="z-font-30 z-font-w z-margin-32">完善个人信息</view>
		  <view class="info-box z-margin-lr-32 z-padding-lr-32 z-padding-tb-8 z-radius-20">
		    <view class="avatar-box z-margin-tb-24 z-padding-tb-12 z-flex-c">
		      <view class="z-font-22 text_666 z-margin-r-32">头像</view>
		      <image class="avatar z-radius" :src="cdn(avatarUrl)"></image>
		      <view class="z-flex-1"></view>
		      <van-icon name="arrow" />
		      <button class="z-absolute" :open-type="chooseAvatar" @chooseavatar="chooseAvatar"></button>
		    </view>
		    <view class="avatar-box z-margin-tb-24 z-flex-c">
		      <view class="z-font-22 text_666 z-margin-r-32">昵称</view>
		      <input type="nickname" class="z-flex-1 z-font-28" v-model="nickname"  placeholder="请输入昵称" @nicknamereview="namereview"/>
		    </view>
		  </view>
		  <view class="z-padding-32" :style="'padding-bottom:' + safeBottom">
		      <view class="z-btn" @tap="saveInfo">保存</view>
		    </view>
		</u-popup>
		

	</view>
</template>

<script>
	import http from "../../utils/http"
	export default {
		data() {
			return {
				agree: false,
				    code: '',
				    avatarUrl: '',
				    nickname: '',
				    show: false,
				    safeBottom: `10px`,
				    config: uni.getStorageSync('config')
			}
		},
		onLoad(options) {
		    this.getCode();
		  },
		methods: {
			 chooseAvatar(e) {
			    const {
			      avatarUrl
			    } = e.detail
			    
			    http.upload(avatarUrl, {
			      type: 'image'
			    }).then(res => {
			      this.avatarUrl = res.data.url
			    })
			  },
			  saveInfo() {
			    http.post('index/updateuserinfo', {
			      type: 'update',
			      avatar: this.avatarUrl,
			      nickname: this.nickname,
			    }, true, false).then(res => {
			      this.show = false
			      this.util.toast(res.msg)
			      uni.reLaunch({
			        url: '/pages/index/index'
			      })
			    })
			  },
			  namereview(e) {
			    console.log(e)
			  },
			  onClose() {
			    this.show = false
			    uni.reLaunch({
			      url: '/pages/index/index'
			    })
			  },
			  getCode() {
			    let that = this
			    uni.login({
			      success(res) {
			        if (res.code) {
			          that.code = res.code
			        } else {
			          this.getCode()
			        }
			      }
			    })
			  },
			
			  read() {
			    if (!this.agree) {
			      uni.showToast({
			        title: '请先阅读并同意《用户协议》《隐私政策》',
			        icon: 'none'
			      })
			    }
			  },
			  infoTap(name,type){
			    let id
			    if(e == 1) {
					let id = this.config.user_agreement_info_id
				}else {
					let id = this.config.user_privacy_info_id
				}
			    util.skip('/service/info/info?id='+id+'&name='+name)
			  },
			  getPhoneNumber(e) {
				  uni.setStorageSync('token','128e8121-d035-4362-9f92-671e3707a4c9')
				  uni.reLaunch({
				    url: '/pages/index/index'
				  })
				  return
				  
			    http.post('user/userlogin', {
			      code: this.code,
			      type: 0,
			      encryptedData: e.detail.encryptedData,
			      iv: encodeURIComponent(e.detail.iv),
			    }, true, false).then(res => {
			      uni.setStorageSync('token', res.data.user.token)
				  uni.setStorageSync('user', res.data.user)
				  uni.setStorageSync('userInfo', res.data.userInfo)
				  
			      if(res.data.userInfo.is_update === 0){
			        this.show = true,
			        this.avatarUrl = res.data.user.avatar,
			        this.nickname = res.data.user.nickname
			      }else{
			        uni.reLaunch({
			          url: '/pages/index/index'
			        })
			      }
			      
			    }).catch(err => {
			      this.getCode()
			    })
			  },
			
			  clickread() {
			    this.agree = !this.agree
			  },
		}
	}
</script>

<style>
/* service/login/login.wxss */
page{
  background-color: #fff;
  position: relative;
}
.logo_img{
  margin-top: 100rpx;
  width: 215rpx;
  height: 215rpx;
}
.title{
  font-size: 50rpx;
  margin-top: 200rpx;
}
.money{
  color: var(--mainmoneycolor);
}
.choose_img{
  width: 30rpx;
  height:30rpx;
}
.btn-box{
  position: relative;
}
.btn{
  width: 686rpx;
  height: 98rpx;
  box-sizing: border-box;
  border: none;
  color: #fff;
  background: #62CB81;
  padding: 30rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 76rpx;
  font-size: 30rpx;
}
.mask{
  position: absolute;
  left: 0;
  top: 0;
  width: 686rpx;
  height: 98rpx;
}


.info-box{
  background-color: #F5F7F9;
}

.avatar-box{
  position: relative;
}


.avatar{
  width: 68rpx;
  height: 68rpx;
}
</style>
